#pragma execution_character_set("utf-8")

#include "w_battery.h"
#include <time.h>
#include <qtimer.h>
#include <qpainter.h>


W_Battery::W_Battery(QWidget *parent):QWidget(parent)
{
    minValue = 0;
    maxValue = 100;
    value = 0;
    alarmValue = 30;
    step = 0.5;

    borderWidth = 5;
    borderRadius = 8;
    bgRadius = 5;
    headRadius = 3;

    borderColorStart = QColor(100,100,100);
    borderColorEnd = QColor(80,80,80);
    alarmColorStart = QColor(250,118,113);
    alarmColorEnd = QColor(204,38,38);
    normalColorStart = QColor(50,205,51);
    normalColorEnd = QColor(60,179,133);

    isForward = false;
    currentValue = 0;

    timer = new QTimer(this);
    timer->setInterval(10);
    connect(timer,SIGNAL(timeout()), this, SLOT(update()));
}

W_Battery::~W_Battery()
{
    if(timer->isActive())
    {
        timer->stop();
    }
}

// 画电池图像
void W_Battery::paintEvent(QPaintEvent *)
{
    //准备绘图工作，启用反锯齿
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    //绘制电池边框
    drawBorder(&painter);
    //绘制电池背景
    drawBg(&painter);
    //绘制电池头部
    drawHead(&painter);
}

//绘制边框
void W_Battery::drawBorder(QPainter *painter)
{
    painter->save();
    double headWidth = width()/15;
    double batteryWidth = width() - headWidth;

    //绘制电池边框
    QPointF topLeft(borderWidth,borderWidth);
    QPointF bottomRight(batteryWidth,height() - borderWidth);
    batteryRect = QRectF(topLeft, bottomRight);
    \
    painter->setPen(QPen(borderColorStart,borderWidth));
    painter->setBrush(Qt::NoBrush);
    painter->drawRoundedRect(batteryRect, borderRadius, borderRadius);

    painter->restore();
}

//绘制电池背景
void W_Battery::drawBg(QPainter *painter)
{
    if(value == minValue) {
        return;
    }
    painter->save();

    //画电池背景渐变色，线性渐变
    QLinearGradient batteryGradient(QPointF(0,0),QPointF(0,height()));
    if(currentValue == alarmValue) {
        batteryGradient.setColorAt(0.0,alarmColorStart);
        batteryGradient.setColorAt(1.0,alarmColorEnd);
    } else {
        batteryGradient.setColorAt(0.0,normalColorStart);
        batteryGradient.setColorAt(1.0,normalColorEnd);
    }

    int margin = qMin(width(), height())/20;
    double unit = (batteryRect.width() - (margin * 2))/100;
    double width = currentValue * unit;
    QPointF topLeft(batteryRect.topLeft().x() + margin, batteryRect.topLeft().y() + margin);
    QPointF bottomRight(width + margin + borderWidth,batteryRect.bottomRight().y() - margin);
    QRectF rect(topLeft, bottomRight);

    painter->setPen(Qt::NoPen);
    painter->setBrush(batteryGradient);
    painter->drawRoundedRect(rect,bgRadius,bgRadius);

    painter->restore();

}

//绘制电池头部
void W_Battery::drawHead(QPainter *painter)
{
    painter->save();

    // 先确定头部小长方体的坐标
    QPointF headRectTopLeft(batteryRect.topRight().x(),height()/3);
    QPointF headRectBottomRight(width(),height() - height()/3);
    QRectF headRect(headRectTopLeft,headRectBottomRight);

    // 电池头部颜色变化
    QLinearGradient headRectGradient(headRect.topLeft(),headRect.bottomRight());
    headRectGradient.setColorAt(0.0, borderColorStart);
    headRectGradient.setColorAt(1.0,borderColorEnd);

    painter->setPen(Qt::NoPen);
    painter->setBrush(headRectGradient);
    painter->drawRoundedRect(headRect, headRadius, headRadius);

    painter->restore();

}

void W_Battery::updateValue()
{

}

double W_Battery::getMinValue() const{}
double W_Battery::getMaxValue() const {}
double W_Battery::getValue()    const {}
double W_Battery::getAlarmValue() const {}

double W_Battery::getStep() const {}
int W_Battery::getBorderWidth() const {}
int W_Battery::getBorderRadius() const {}
int W_Battery::getBgRadius() const {}
int W_Battery::getHeadRadius() const {}

QColor W_Battery::getBorderColorStart() const {}
QColor W_Battery::getBorderColorEnd() const {}

QColor W_Battery::getAlarmColorStart() const {}
QColor W_Battery::getAlarmColorEnd() const {}

QColor W_Battery::getNormalColorStart() const {}
QColor W_Battery::getNormalColorEnd() const {}

QSize W_Battery::sizeHint() const {}
QSize W_Battery::mininumSizeHint() const {}

void W_Battery::setRange(double minValue, double maxValue){}
void W_Battery::setRange(int minValue, int maxValue){}

void W_Battery::setMinValue(double minValue){}
void W_Battery::setMaxValue(double maxValue){}

void W_Battery::setValue(double value){}
void W_Battery::setValue(int value){}

void W_Battery::setAlarmValue(double alarmValue){}
void W_Battery::setAlarmValue(int alarmValue){}

void W_Battery::setStep(double step){}
void W_Battery::setStep(int step){}

void W_Battery::setBorderWidth(int borderWidth){}
void W_Battery::setBorderRadius(int borderRadius){}
void W_Battery::setBgRadius(int bgRadius){}
void W_Battery::setHeadRadius(int headRadius){}

void W_Battery::setBorderColorStart(const QColor &borderColorStart){}
void W_Battery::setBorderColorEnd(const QColor &borderColorEnd){}

void W_Battery::setAlarmColorStart(const QColor &alaramColorStart){}
void W_Battery::setAlarmColorEnd(const QColor &alaramColorEnd){}

void W_Battery::setNormalColorStart(const QColor &normalColorStart){}
void W_Battery::setNormalColorEnd(const QColor &normalColorEnd){}
